package com.xyht.sca_s.student_manage_system.modules.websocket.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.xyht.sca_s.student_manage_system.modules.websocket.WebSocketCommunication;
import com.xyht.sca_s.student_manage_system.modules.websocket.entity.WebsocketEntity;
import com.xyht.sca_s.student_manage_system.modules.websocket.entity.WebsocketResult;
import lombok.extern.log4j.Log4j2;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Log4j2
public class WebSocketService {

    public void singleSend(String user_id,Integer client_flag, WebsocketResult data) {
        log.debug("websocket消息发送++++++++++"+user_id);
        try {
            if (client_flag != null){
                WebSocketCommunication webSocketCommunication = WebSocketCommunication.webSocketServerMAP.get(user_id+"_"+client_flag);
                if (webSocketCommunication != null){
                    log.debug("websocket消息发送++++++++++"+user_id+"_"+client_flag);
                    webSocketCommunication.session.getBasicRemote().sendText(JSONObject.toJSONStringWithDateFormat(data,"yyyy-MM-dd HH:mm:ss"));
                }

            }else{
                WebSocketCommunication webSocketCommunication1 = WebSocketCommunication.webSocketServerMAP.get(user_id+"_"+1);
                if (webSocketCommunication1 != null){
                    log.debug("websocket消息发送++++++++++1"+user_id);
                    webSocketCommunication1.session.getBasicRemote().sendText(JSONObject.toJSONStringWithDateFormat(data,"yyyy-MM-dd HH:mm:ss"));
                }

                WebSocketCommunication webSocketCommunication2 = WebSocketCommunication.webSocketServerMAP.get(user_id+"_"+2);
                if (webSocketCommunication2 != null){
                    log.debug("websocket消息发送++++++++++2");
                    webSocketCommunication2.session.getBasicRemote().sendText(JSONObject.toJSONStringWithDateFormat(data,"yyyy-MM-dd HH:mm:ss"));
                }

                WebSocketCommunication webSocketCommunication3 = WebSocketCommunication.webSocketServerMAP.get(user_id+"_"+3);
                if (webSocketCommunication3 != null){
                    log.debug("websocket消息发送++++++++++3");
                    webSocketCommunication3.session.getBasicRemote().sendText(JSONObject.toJSONStringWithDateFormat(data,"yyyy-MM-dd HH:mm:ss"));
                }

                WebSocketCommunication webSocketCommunication4 = WebSocketCommunication.webSocketServerMAP.get(user_id+"_"+4);
                if (webSocketCommunication4 != null){
                    log.debug("websocket消息发送++++++++++4");
                    webSocketCommunication4.session.getBasicRemote().sendText(JSONObject.toJSONStringWithDateFormat(data,"yyyy-MM-dd HH:mm:ss"));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public void batchSend (List<String> userList,Integer client_flag, WebsocketResult data ){
        if (userList == null || userList.size()<1){
            return;
        }
        for (String user_id:userList){
            singleSend(user_id,client_flag,data);
        }
    }


}
